new Vue({
  el: "#app",
  data() {
    return {
      fileList: [],
      rules: [],
      form: {
        Company_Honors_SerialNumber: "",
        Company_Honors_Name: "",
        Picture_URL: "",
        Company_Honors_Date: "",
      },
      loading: false,
    };
  },
  mounted() {
    const Company_Honors_SerialNumber = sessionStorage.getItem(
      "Company_Honors_SerialNumber"
    );
    this.getData(Company_Honors_SerialNumber);
  },
  methods: {
    handleRemove(file, fileList) {
      //删除时保留一个框
      this.fileList.splice(0, 1);
    },
    handleBeforeUpload(file, fileList) {
      // 生成预览URL并添加到fileList中
      const reader = new FileReader();
      reader.onload = (e) => {
        //调用存储的接口放在onload中，存在异步操作
        const newFile = {
          name: file.name,
          url: e.target.result, // 预览URL
          uid: file.uid, //添加uid
          // 可以添加其他属性，如上传状态等
        };
        this.fileList.push(newFile);
        console.log(this.fileList);

        // return;
        $.ajax({
          // headers: requestHeaders(), // 确保这个函数返回正确的头部信息
          url: "https://service.kdddy.cn/donghuiForRequest/store/photo",
          // url: "http://192.168.10.79:8087/store/photo",
          type: "POST", // 注意HTTP方法应该大写
          contentType: "application/json", // 设置请求体的媒体类型为application/json
          data: JSON.stringify(this.fileList), // 将数据序列化为JSON字符串
          success: function (Result) {
            console.log(Result);
          },
          complete: function (XMLHttpRequest) {
            requestPretreatment(XMLHttpRequest); // 确保这个函数在config/config.js中定义
          },
          dataType: "json", // 预期服务器返回的数据类型
        });
      };
      reader.readAsDataURL(file);
      // 返回false以阻止自动上传

      return false;
    },
    getData(Primary_Key) {
      console.log(Primary_Key);
      //   return;
      const _this = this;
      $.ajax({
        // url: DomainName + '/getInquiryIDOriginal?token=' + $.cookie('token'),
        url: "https://service.kdddy.cn/donghui/generalBusiness/generalQueryByTableNameWithOriginal",
        type: "POST",
        data: {
          Data: "company_honors", //表名
          Condition: "Company_Honors_SerialNumber='" + Primary_Key + "'",
        },
        success: function (res) {
          console.log(res);
          _this.form = res[0];
          let lArray_URL = new Array();
          let lJSONObject_Picture_Record = new Object();
          lJSONObject_Picture_Record.url = `https://www.kdddy.cn/Image/${res[0].Picture_URL}`;
          lArray_URL.push(lJSONObject_Picture_Record);
          console.log(lArray_URL);

          _this.fileList = lArray_URL;
        },
        error: function () {
          _this.$message.error("操作失败，请稍后再试");
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false;
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href =
              absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
      });
    },
    submitForm(formName) {
      console.log(this.form);
      let lArray = new Array();
      lArray.push(this.form);
      let lJSONObject_company_honors = new Object();
      lJSONObject_company_honors.company_honors = lArray; //换表
      let lJSONObject_DataSet = new Object(); // Modify
      lJSONObject_DataSet.Modify = lJSONObject_company_honors; //换表
      const _this = this;
      $.ajax({
        // headers: requestHeaders(),
        // url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
        url: "https://service.kdddy.cn/donghui/clientOperation/generalSaveForClient",
        type: "POST",
        data: {
          Data: JSON.stringify(lJSONObject_DataSet),
        },
        success: function (Result) {
          console.log(Result);
          if (Result.Status === "success") {
            _this.$message({
              message: "提交成功",
              type: "success",
            });
            window.history.go(-1);
          } else {
            _this.$message.error("提交失败，请稍后再试");
          }
        },
        complete: function (XMLHttpRequest) {
          //   requestPretreatment(XMLHttpRequest); //config/config.js 响应预检
        },
        dataType: "json",
      });
      return;

      this.$refs[formName].validate((valid) => {
        if (valid) {
          _this.loading = true;
          const { add, modify } = _this.deal();
          let data = {};
          if (add == null) {
            data = {
              Data: JSON.stringify({ Modify: { company_honors: [modify] } }),
            };
          } else {
            data = {
              Data: JSON.stringify({
                Modify: { company_honors: [modify] },
                Add: { company_honors: [add] },
              }),
            };
          }
          $.ajax({
            url: DomainName + "/getEditAction?token=" + $.cookie("token"),
            type: "POST",
            data: { ...data, Operation_Name: "修改公司简介" },
            success: function (res) {
              if (res.Status === "success") {
                _this.$message.success("提交成功");
                window.history.go(-1);
              } else {
                _this.$message.error("提交失败，请稍后再试");
              }
            },
            error: function () {
              _this.$message.error("操作失败，请稍后再试");
            },
            complete: function (XMLHttpRequest) {
              _this.loading = false;
              if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                window.parent.document.location.href =
                  absolutepath() +
                  XMLHttpRequest.getResponseHeader("CONTENTPATH");
              }
            },
          });
        } else {
          return false;
        }
      });
    },
    deal() {
      const modify = deepClone(this.form);
      return { add: null, modify };
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
});
